package javax.xianfeng.drools.hello;

import javax.xianfeng.drools.config.DroolsConfig;
import javax.xianfeng.drools.util.DroolsUtils;

import org.junit.Test;
import org.kie.api.io.Resource;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;
import org.kie.internal.utils.KieHelper;

import lombok.extern.slf4j.Slf4j;

/**
 * https://blog.csdn.net/chenhao_c_h/article/details/85090307
 * 
 * @since 2020/04/04 09:03:54
 */
@Slf4j
public class HelloTest2 {

	/**
	 * 执行drl文件（指定文件）
	 * 
	 * @since 2020/04/07 21:40:32
	 */
	@Test
	public void hello() {
		// 关联文件
		String filePath = DroolsConfig.RULE_ROOT_PATH + "/hello/welcome.drl";
		Resource resource = ResourceFactory.newClassPathResource(filePath);

		KieHelper kieHelper = new KieHelper();
		kieHelper.addResource(resource, ResourceType.DRL);
		KieSession kieSession = DroolsUtils.createSession(kieHelper);
		try {
			kieSession.fireAllRules();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			DroolsUtils.closeSession(kieSession);
		}

	}

}
